/**
 * Copyright (C) 2011 Andrew C. Love (DNC) <dnc.app.sup@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*****************************************************************************/
//VelocityProcessor
/*****************************************************************************/
package com.dnc.tools.gen;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/*****************************************************************************/
//VelocityProcessor
/*****************************************************************************/
public class VelocityProcessor {
    private static Logger log = Logger.getLogger(VelocityProcessor.class);
	private static final String CONTEXT = "cloak";
    private static VelocityEngine m_velocityEngine = new VelocityEngine();
    private static boolean m_engineInit = false;

	public void setVelocityProperty(String name, String value) {
	    log.debug("velocity setProperty " + name +":"+value);
	    m_velocityEngine.setProperty(name,value);
    }

    public String processTemplate(String templateFilename, Map mergeProperties)  {        
        log.info("Attempting to load :" + templateFilename);
       
        initVelocityEngine();            
        
        VelocityContext vc = new VelocityContext();
        vc.put(CONTEXT, mergeProperties);        
        Template template = (Template)createTemplate(templateFilename);
        StringWriter mergedTemplateWriter = mergeTemplate(template, vc);
        
        return mergedTemplateWriter.getBuffer().toString();
    }    

    protected Template createTemplate(String templateName) {
        return m_velocityEngine.getTemplate(templateName);    
    }

    protected StringWriter mergeTemplate(Template template, VelocityContext velocityContext) {
        StringWriter writer = new StringWriter();
        template.merge(velocityContext, (Writer)writer);
        log.info(writer.getBuffer());        
        return writer;
    }
    
    private synchronized void initVelocityEngine() {
        if (m_engineInit == false) {            
            Properties properties = new Properties();
            properties.put("resource.loader", "class");
            properties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

            m_velocityEngine.init(properties);
            m_engineInit = true;           
            
        }
    } 
    
    public static void main(String[] args) {
        VelocityProcessor vp = new VelocityProcessor();
        System.out.println("hi");
    }

}

